home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Doors_System / XPR / XPRD_Source / main.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  9KB  |  324 lines

  1. /*
  2.     XPR
  3.     ---
  4.     eXternal PRotocol Driver
  5.  
  6.     written & (C) 1990 by Oliver Wagne
  7.  
  8. */
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <devices/serial.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <time.h>
  16. #include <stdlib.h>
  17. #ifdef V030
  18. #include <exec/execbase.h>
  19. #endif
  20. #include "xpr.h"
  21.  
  22.  
  23. extern void sprintf(char*,...);
  24.  
  25. short os13;
  26.  
  27. #define print(s) Write(Output(),s,strlen(s))
  28.  
  29. #define INFOSTRING "XPRD version V1.13; last compiled " __TIME__ " on " __DATE__ "\n\
  30. Written and copyright © 1991 by Oliver Wagner, All Rights Reserved\n\
  31. Contact:\n\033[b33;1m\
  32.   EMail : O.WAGNER@LINK-ESW.ZER (Z-Netz)\n\
  33.     O.WAGNER@LINK-ESW.ZER.SUB.ORG (Internet)\n\
  34.     Oliver Wagner of 2:240/5600 (Wauwaunet)\n\
  35.   Voice : (++49) (0)5659/518\n\
  36.   Data  : (++49) (0)5659/1307 (24h DST) SUPPORT-BBS\n\
  37.         Snail : Oliver Wagner\n\
  38.                 Auf Dem Stein 5\n\
  39.                 3441 Weißenborn\n\
  40.     West Germoney\n\033[0m"
  41.  
  42. extern char *xprfilearray[];
  43. struct Library *XProtocolBase,*IntuitionBase,*GfxBase;
  44.  
  45. char *libname="xprzmodem.library";
  46. char *options=0;
  47. char *devname="serial.device";
  48. short devnum=0;
  49. short nodelay=0;
  50. struct Screen *firstscreen=0;
  51. long rbuflen=0;
  52. short sharedaccess=0,rtscts=0;
  53. char *tempdir=0;
  54. long speed=0;
  55. short checkcarrier=0;
  56. short windowx=60,windowy=25;
  57. char *logfile=0;
  58. BPTR logfilep;
  59. char *errorfile=0;
  60. short nowindow=0;
  61.  
  62. void exit(int);
  63. void initdisplay(void);
  64. long xpr_chk4abort(void);
  65.  
  66. struct IOExtSer *xpr_serio;
  67. static struct IOExtSer ios;
  68. static struct MsgPort *smp;
  69.  
  70. static int openserial(char *dev,int unit)
  71. {
  72.     if(!(smp=CreatePort(0,0))) return(-1);
  73.     ios.IOSer.io_Message.mn_ReplyPort=smp;
  74.     if(sharedaccess) ios.io_SerFlags|=SERF_SHARED;
  75.     if(OpenDevice(dev,unit,(struct IORequest *)&ios,0)) {
  76.   DeletePort(smp);
  77.   return(-1);
  78.     }
  79.     xpr_serio=&ios;
  80.     if(rbuflen||speed||rtscts) {
  81.   if(rbuflen) ios.io_RBufLen=rbuflen;
  82.   if(speed) ios.io_Baud=speed;
  83.   if(rtscts) ios.io_SerFlags|=SERF_7WIRE;
  84.   ios.IOSer.io_Command=SDCMD_SETPARAMS;
  85.   DoIO((struct IORequest *)&ios);
  86.     }
  87.     ios.IOSer.io_Command=SDCMD_QUERY;
  88.     DoIO((struct IORequest *)&ios);
  89.     speed=ios.io_Baud;
  90.     rtscts=ios.io_SerFlags&SERF_7WIRE;
  91.     return(0);
  92. }
  93. static void closeserial(void)
  94. {
  95.     CloseDevice((struct IORequest *)&ios);
  96.     DeletePort(smp);
  97. }
  98.  
  99. static char logbf[160];
  100. void __stdargs plog(char typ,char *string,long arg1,long arg2)
  101. {
  102.   long t;
  103.   if(logfilep) {
  104.     t=time(0);
  105.     sprintf(logbf,"%lc %24.24s : ",(long)typ,ctime(&t));
  106.     Write(logfilep,logbf,strlen(logbf));
  107.     sprintf(logbf,string,arg1,arg2);
  108.     Write(logfilep,logbf,strlen(logbf));
  109.   }
  110. }
  111.  
  112. int __stdargs closelog(int x)
  113. {
  114.   BPTR f;
  115.   plog('!',"Exiting XPRD\n",0,0);
  116.   if(logfilep) Close(logfilep);
  117.   sprintf(logbf,"%-2ld\n",x);
  118.   if(errorfile) {
  119.     if(f=Open(errorfile,MODE_NEWFILE)) {
  120.       Write(f,logbf,3);
  121.       Close(f);
  122.     }
  123.   }
  124.   return(x);
  125. }
  126. void main(int argc, char**argv)
  127. {
  128.     int c=1,l=0,com,retval=20;
  129.     BPTR templock=0,oldlock=0;
  130.     char *help,ch1,ch2;
  131.  
  132. //    onexit(closelog);
  133.     IntuitionBase=OpenLibrary("intuition.library",36);
  134.     if(!IntuitionBase) {
  135.     IntuitionBase=OldOpenLibrary("intuition.library");
  136.     os13=1;
  137.     }
  138.     GfxBase=OldOpenLibrary("graphics.library");
  139.     print("\033[32;1m_____________________________________________________________________________\n\n\
  140.  \033[0;33;3meXternal PRotocol Driver\033[0m V1.13 by \033[1mOliver Wagner\033[0m © 1991, All Rights Reserved\n\
  141. \033[32;1m_____________________________________________________________________________\n\n\033[0m");
  142. #ifdef V030
  143.     {
  144.         struct ExecBase *eb=*((struct ExecBase**)4);
  145.         print("\t\t\t68020/30/40 \033[33mONLY\033[0m VERSION\n\n");
  146.         if(!(eb->AttnFlags&AFF_68020)) {
  147.     print("\x07");
  148.     exit(20);
  149.   }
  150.     }
  151. #endif
  152.     if(argc<2||*argv[1]=='?')
  153.     {
  154.       print("Usage: XPRD [-options] s[end]|r[eceive] [file1 [file2 [file3...]]]\navaible options:\n"
  155.             "-l\033[3mlibname \033[0m   - define XPR library (def.: \"xprzmodem.library\")\n"
  156.             "-o\033[3moptions \033[0m   - define XPR startup options string\n"
  157.             "-d\033[3mdevname \033[0m   - define IO device (def.: \"serial.device\")\n"
  158.             "-u\033[3munitnum \033[0m   - define IO device unit (def.: 0)\n"
  159.             "-x\033[3m        \033[0m   - rtscts\n"
  160.             "-s\033[3m        \033[0m   - shared\n"
  161.             "-t\033[3mdirname \033[0m   - define temporary dir for batch dl\n");
  162.       print("-b\033[3mbufsize \033[0m   - define IO device read buffer size (K)\n"
  163.             "-p\033[3mx,y \033[0m       - define window X and Y position\n"
  164.             "-m\033[3mlogfile \033[0m   - enable log option (def.: \"xprd.log\")\n"
  165.             "-r\033[3merrfile \033[0m   - define return code output file\n"
  166.             "-c       - enable carrier detect\n"
  167.             "-n       - no delay after EOT\n"
  168.             "-f       - open window on frontmost screen (DANGER!)\n"
  169.             "-q       - don't open window; quiet mode\n"
  170.             "-i       - display version and copyright information\n"
  171.             "--<baud> - set baud rate\n");
  172.  
  173.       exit(0);
  174.     }
  175.     while(argv[c][0]=='-')
  176.     {
  177.       switch(tolower(argv[c][1]))
  178.       {
  179.         case 'l':
  180.           libname=&argv[c][2];
  181.           break;
  182.         case 'o':
  183.           options=&argv[c][2];
  184.           break;
  185.         case 'd':
  186.           devname=&argv[c][2];
  187.           break;
  188.         case 'u':
  189.           devnum=atoi(&argv[c][2]);
  190.           break;
  191.         case 'b':
  192.           rbuflen=atoi(&argv[c][2])<<10;
  193.           break;
  194.         case 's':
  195.           sharedaccess=1;
  196.           break;
  197.         case '-':
  198.           speed=atoi(&argv[c][2]);
  199.           break;
  200.         case 'x':
  201.           rtscts=1;
  202.           break;
  203.         case 'n':
  204.           nodelay=1;
  205.           break;
  206.         case 'p':
  207.           help=&argv[c][2];
  208.           if(*help==',')
  209.           {
  210.             windowy=atoi(&help[1]);
  211.             break;
  212.           }
  213.           windowx=atoi(help);
  214.           while(*help && *help!=',') help++;
  215.           if(*help==',') windowy=atoi(&help[1]);
  216.           break;
  217.         case 'f':
  218.           firstscreen=(struct Screen*)-1;
  219.           if(strlen(argv[c])==10) stch_l(&argv[c][2],(long *)&firstscreen);
  220.           if(!firstscreen) nowindow=1;
  221.           break;
  222.         case 't':
  223.           tempdir=&argv[c][2];
  224.           break;
  225.         case 'c':
  226.           checkcarrier=1;
  227.           break;
  228.         case 'i':
  229.           print(INFOSTRING);
  230.           exit(0);
  231.         case 'm':
  232.           if(strlen(argv[c])>2)
  233.           {
  234.             logfile=&argv[c][2];
  235.           }
  236.           else
  237.           {
  238.             logfile="XPRD.log";
  239.           }
  240.           if(!(logfilep=Open(logfile,MODE_OLDFILE)))
  241.           {
  242.             if(!(logfilep=Open(logfile,MODE_NEWFILE)))
  243.             {
  244.               print("Unable to open logfile!\x07\n");
  245.               exit(20);
  246.             }
  247.           }
  248.           else
  249.           {
  250.             Seek(logfilep,0,OFFSET_END);
  251.           }
  252.           plog('!',"Started XPRD\n",0,0);
  253.           break;
  254.         case 'r':
  255.           errorfile=&argv[c][2];
  256.           break;
  257.         case 'q':
  258.           nowindow=1;
  259.           break;
  260.         default:
  261.           print("Illegal option: '");
  262.           print(&argv[c][1]);
  263.           print("'!\x07\n");
  264.           exit(20);
  265.           break;
  266.       }
  267.       c++;
  268.       if(c==argc)
  269.       {
  270.         print("Missing SEND or RECEIVE!\n\x07");
  271.         exit(20);
  272.       }
  273.     }
  274.     if(tolower(*argv[c])!='s'&&tolower(*argv[c])!='r')
  275.     {
  276.       print("Missing SEND or RECEIVE!\n\x07");
  277.       exit(20);
  278.     }
  279.     com=(tolower(*argv[c])=='s')?1:0;
  280.     c++;
  281.     if(c==argc) xprfilearray[l++]="NAPFSUELZE";
  282.     else while(c!=argc) xprfilearray[l++]=argv[c++];
  283.     xprfilearray[l]=0;
  284.  
  285.     if(!(XProtocolBase=OldOpenLibrary(libname))) {
  286.   print("Failed to open XPR library!\n\x07");
  287.   exit(20);
  288.     }
  289.     if(openserial(devname,devnum)) {
  290.   print("Failed to open IO device!\n\x07");
  291.   CloseLibrary(XProtocolBase);
  292.   exit(20);
  293.     }
  294.     if(xprinit(options)) {
  295.   print("Failed to init XPR library!\n\x07");
  296.   CloseLibrary(XProtocolBase);
  297.   closeserial();
  298.   exit(20);
  299.     }
  300.     if(tempdir) {
  301.   if(!(templock=Lock(tempdir,SHARED_LOCK))) {
  302.       print("Failed to Lock() tempory dir!\n\x07");
  303.       goto xit;
  304.   }
  305.   if(!(oldlock=CurrentDir(templock))) {
  306.       print("Can't CurrentDir() to temporary dir!\n\x07");
  307.       goto xit;
  308.   }
  309.     }
  310.     if(nowindow) print("Doing transfer in quiet mode\n");
  311.     initdisplay();
  312.     if(!xpr_chk4abort()) {
  313.   retval=(((com)?xprsend():xprreceive())==1)?0:20;
  314.     }
  315.     if(!nodelay) Delay(136);
  316. xit:
  317.     if(oldlock) CurrentDir(oldlock);
  318.     if(templock) UnLock(templock);
  319.     xprclose();
  320.     closeserial();
  321.     CloseLibrary(XProtocolBase);
  322.     exit(retval);
  323. }
  324.